package com.fourth.controller.api;

import com.fourth.service.UbCartService;
import com.github.pagehelper.PageInfo;
import com.ljx.entity.Cart;
import com.ljx.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Mr.ma
 * @since 2023/3/2 14:36
 **/
@RestController
@RequestMapping("/detail/cart")
@Api("购物车操作接口")
public class UbApiCartController {

    @Resource
    private UbCartService ubCartService;

    @RequestMapping("findCart/{currentPage}/{pageSize}")
    @ApiOperation("查询登录用户购物车中的所有商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "页码", dataType = "int" , defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int" , defaultValue = "5")
    })
    public Result<PageInfo<Cart>> findCart(@PathVariable("currentPage") Integer currentPage,@PathVariable("pageSize") Integer pageSize){
        PageInfo<Cart> goodsList = ubCartService.findCart(currentPage,pageSize);
        return new Result<>(2000,"查询成功",goodsList);
    }

    @RequestMapping("addCart")
    @ApiOperation("添加商品到购物车")
    @ApiImplicitParam(name = "cart", value = "购物车对象", dataType = "Cart")
    public Result addCart(@RequestBody Cart cart){
        int row = ubCartService.insert(cart);
        if (row > 0){
            return new Result(2000,"添加成功",null);
        }
        return new Result(5000,"添加失败",null);
    }

    @RequestMapping("deleteCart/{id}")
    @ApiOperation("从购物车中删除商品")
    public Result deleteCart(@PathVariable String id){
        int row = ubCartService.deleteCart(id);
        if (row > 0){
            return new Result(2000,"删除成功",null);
        }
        return new Result(5000,"删除失败",null);
    }

    @RequestMapping("deleteByIds")
    @ApiOperation("从购物车中删除所有商品")
    public Result deleteByIds(@RequestBody String[] ids){
        int row = ubCartService.deleteByIds(ids);
        if (row > 0){
            return new Result(2000,"删除成功",null);
        }
        return new Result(5000,"删除失败",null);
    }
}
